package com.vmove.childtrain.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.vmove.childtrain.R;
import com.vmove.childtrain.common.helper.OnRecyclerItemClickListener;
import com.vmove.childtrain.db.dao.UserDao;
import com.vmove.childtrain.db.entity.User;
import com.vmove.childtrain.utils.ToastUtils;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 用户列表
 */
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ListHolder> {

    Context context;
    List<User> datas;
    OnRecyclerItemClickListener listener;
    UserDao userDao;

    public void setOnItemClickListener(OnRecyclerItemClickListener listener){
        this.listener = listener;
    }

    public UserAdapter(Context context, List<User> ds) {
        this.context = context;
        this.datas = ds;
        userDao = new UserDao(context);
    }

    public void setData(List<User> ds){
        this.datas=ds;
        this.notifyDataSetChanged();
    }

    public List<User> getDatas() {
        return datas;
    }

    @Override
    public ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_user, parent, false);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(ListHolder holder, int position) {
        holder.setData(position);

    }

    @Override
    public int getItemCount() {
        if (null==datas || datas.size()==0)
            return 0;
        return datas.size();
    }

    class ListHolder extends RecyclerView.ViewHolder{
        private TextView mTvName;
        private ImageView mIvSex;
        private TextView mTvBirth;
        private TextView mTvHeight;
        private TextView mTvWeight;
        private TextView mTvTeam;
        private User data;
        private int position;

        public ListHolder(View itemView) {
            super(itemView);
            mTvName = itemView.findViewById(R.id.tv_name);
            mTvBirth = itemView.findViewById(R.id.tv_birth);
            mTvHeight = itemView.findViewById(R.id.tv_height);
            mTvWeight = itemView.findViewById(R.id.tv_weight);
            mTvTeam = itemView.findViewById(R.id.tv_team);
            mIvSex = itemView.findViewById(R.id.iv_sex);
        }

        public void setData(final int position) {
            final User s = datas.get(position);
            data = s;
            this.position = position;
            mTvName.setText(s.getUsername()+"");
            mIvSex.setImageResource(s.getSex()==0?R.drawable.ic_boy:R.drawable.ic_girl);
            mTvHeight.setText(s.getHeight()+"");
            mTvWeight.setText(s.getWeight()+"");
            mTvBirth.setText(s.getBirthYear()+"/"+s.getBirthMonth());
            mTvTeam.setText(s.getTeam().getTeamname()+"");
        }

    }

}
